package com.canxi.earn.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.canxi.earn.domain.bo.CxJokeCategoryBo;
import com.canxi.earn.domain.vo.CxJokeCategoryVo;
import com.canxi.earn.domain.CxJokeCategory;
import com.canxi.earn.mapper.CxJokeCategoryMapper;
import com.canxi.earn.service.ICxJokeCategoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import com.canxi.cxcommon.utils.PageUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.hutool.core.util.StrUtil;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;

import com.canxi.common.core.page.TableDataInfo;

/**
 * 笑话分类Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-05
 */
@RequiredArgsConstructor
@Service
public class CxJokeCategoryServiceImpl  extends JoinServiceImpl<CxJokeCategoryMapper, CxJokeCategory>  implements ICxJokeCategoryService {

    private final CxJokeCategoryMapper baseMapper;

    @Override
    public TableDataInfo<CxJokeCategoryVo> queryCxJokeCategoryList(CxJokeCategoryBo cxJokeCategoryBo) {

        return PageUtils.buildDataInfo(this.baseMapper.queryCxJokeCategoryList(PageUtils.buildPage(), BeanUtil.toBean(cxJokeCategoryBo, CxJokeCategoryVo.class)));
    }

    @Override
    public List<CxJokeCategoryVo> queryCxJokeCategoryAll(CxJokeCategoryBo cxJokeCategoryBo) {
        return this.baseMapper.queryCxJokeCategoryList(BeanUtil.toBean(cxJokeCategoryBo, CxJokeCategoryVo.class));
    }

    /**
     * 获取tree
     * @param cxJokeCategoryBo
     * @return
     */
    public List<Tree<String>> queryCxJokeCategoryTree(CxJokeCategoryBo cxJokeCategoryBo) {
        List<CxJokeCategoryVo> list = this.baseMapper.queryCxJokeCategoryList(BeanUtil.toBean(cxJokeCategoryBo, CxJokeCategoryVo.class));
            TreeNodeConfig nodeConfig = new TreeNodeConfig();
            nodeConfig.setIdKey("id");
            nodeConfig.setParentIdKey("pid");
            nodeConfig.setChildrenKey("children");
            nodeConfig.setNameKey("name");
            nodeConfig.setDeep(4);
        List<Tree<String>> build = TreeUtil.build(list, "0", nodeConfig, (treeNode, tree) -> {
            tree.setId(treeNode.getId().toString());
            tree.setParentId(treeNode.getPid().toString());
            tree.setName(treeNode.getName());
            tree.putExtra("label",treeNode.getName());
            tree.putExtra("value",treeNode.getId());
        });
        return build;
    }


    /**
     * 查询笑话分类
     */
    @Override
    public CxJokeCategoryVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询笑话分类列表
     */
    @Override
    public List<CxJokeCategoryVo> queryList(CxJokeCategoryBo bo) {
        JoinLambdaWrapper<CxJokeCategory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private JoinLambdaWrapper<CxJokeCategory> buildQueryWrapper(CxJokeCategoryBo bo) {
        Map<String, Object> params = bo.getParams();
        JoinLambdaWrapper<CxJokeCategory> lqw = joinLambdaQueryWrapper();
        lqw.eq(StrUtil.isNotBlank(bo.getPid()), CxJokeCategory::getPid, bo.getPid());
        lqw.eq(StrUtil.isNotBlank(bo.getPids()), CxJokeCategory::getPids, bo.getPids());
        lqw.like(StrUtil.isNotBlank(bo.getName()), CxJokeCategory::getName, bo.getName());
        lqw.eq(StrUtil.isNotBlank(bo.getStatus()), CxJokeCategory::getStatus, bo.getStatus());
        lqw.eq(bo.getSort() != null, CxJokeCategory::getSort, bo.getSort());
        return lqw;
    }

    /**
     * 新增笑话分类
     */
    @Override
    public Boolean insertByBo(CxJokeCategoryBo bo) {
        CxJokeCategory add = BeanUtil.toBean(bo, CxJokeCategory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改笑话分类
     */
    @Override
    public Boolean updateByBo(CxJokeCategoryBo bo) {
        CxJokeCategory update = BeanUtil.toBean(bo, CxJokeCategory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CxJokeCategory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除笑话分类
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
